﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DentScan2.WebApp
{
    public partial class Calendar : Classes.BaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogged();

            if (!IsPostBack)
            {                
                List<DateTime> repairDates = (from d in dl.Damages
                                              where d.RepairDate.HasValue
                                              orderby d.RepairDate descending
                                              select d.RepairDate.Value).Distinct().ToList();
                Session["RepairDates"] = repairDates;
                calendar.SelectedDates.Add(DateTime.Now);
                Search();
                //calendar.SelectedDates.Clear();
                //foreach(DateTime dt in repairDates)
                //    calendar.SelectedDates.Add(dt);



                //if(repairDates.Count > 0)
                //    calendar.VisibleDate = repairDates[0];
            }
        }

        protected void calendar_DayRender(object sender, DayRenderEventArgs e)
        {
            List<DateTime> repairDates = (List<DateTime>)Session["RepairDates"];
            if (repairDates.Contains(e.Day.Date))
                e.Cell.BackColor = System.Drawing.Color.Orange;
        }

        protected void calendar_SelectionChanged(object sender, EventArgs e)
        {
            Search();

        }

        

        

        private void Search()
        {
            SelectedDatesCollection dates = calendar.SelectedDates;
            List<DateTime> lDates = new List<DateTime>();
            foreach (DateTime d in dates)
                lDates.Add(d);
            //var result = dl.Damages.Where(d => d.RepairDate.HasValue && lDates.Contains(d.RepairDate.Value));
            var result = (from d in dl.Damages
                          join car in dl.Cars on d.CarId equals car.CarId
                          join owner in dl.Owners on car.OwnerId equals owner.OwnerId
                          join model in dl.Models on car.ModelId equals model.ModelId
                          join make in dl.Makes on model.MakeId equals make.MakeId
                          where d.RepairDate.HasValue && lDates.Contains(d.RepairDate.Value) && (d.UserId == this.UserId || this.Level == 1)
                          select new
                          {
                              DamageId = d.DamageId,
                              MakeName = make.MakeName,
                              CarId = car.CarId,
                              OwnerId = car.OwnerId,
                              ModelName = model.ModelName,
                              RegNumber = car.RegNumber,
                              Name = owner.Name,
                              Total = d.Total,
                              ExpertiseDate = d.ExpertiseDate
                          }).ToList();
            if (result.Count() == 0)
            {
                rResults.Visible = false;
            }
            else
            {
                rResults.Visible = true;
                rResults.DataSource = result;
                rResults.DataBind();
            }
            lRecordCount.Text = "Общ брой: " + result.Count().ToString();
 
        }
    }
}